<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
	xmlns:datetime="http://exslt.org/dates-and-times">
	<xsl:output method="xml" encoding="UTF-8"/>
	<xsl:param name="exist-url"/>

	<xsl:param name="eadheader_url">
		<xsl:value-of select="document(concat($exist-url, '/site_info.xml'))//eadheader_url"/>
	</xsl:param>
	<xsl:param name="archdesc_did_url">
		<xsl:value-of select="document(concat($exist-url, '/site_info.xml'))//archdesc_did_url"/>
	</xsl:param>

	<xsl:template match="/">
		<add>
			<xsl:apply-templates select="descendant::node()[@level='item']"/>
		</add>
	</xsl:template>

	<xsl:template match="c[@level='item']">

		<xsl:variable name="id" select="@id"/>

		<doc>
			<field name="id">
				<xsl:value-of select="$id"/>
			</field>
			<field name="timestamp">
				<xsl:variable name="timestamp" select="datetime:dateTime()"/>
				<xsl:choose>
					<xsl:when test="contains($timestamp, 'Z')">
						<xsl:value-of select="$timestamp"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="concat($timestamp, 'Z')"/>
					</xsl:otherwise>
				</xsl:choose>
			</field>
			<field name="source_meta">
				<xsl:text>ead</xsl:text>
			</field>
			<field name="institution_code">
				<xsl:value-of select="document($eadheader_url)//eadid"/>
			</field>
			<field name="institution_facet">
				<xsl:value-of select="document($eadheader_url)//publisher"/>
			</field>

			<xsl:for-each
				select="ancestor-or-self::node()[@level='series'] | ancestor-or-self::node()[@level='collection']">
				<field name="collection_facet">
					<xsl:value-of select="did/unittitle"/>
				</field>
			</xsl:for-each>

			<field name="collection_facet">
				<xsl:value-of select="document($archdesc_did_url)//unittitle"/>
			</field>

			<!-- add each bibliographic reference into the index -->
			<xsl:apply-templates select="bibliography/bibref"/>

			<xsl:apply-templates select="did"/>

			<xsl:for-each select="descendant::persname | descendant::geogname">
				<xsl:variable name="role">
					
						<xsl:if test="@role = 'city' or @role = 'region' or @role = 'state' or @role = 'deity' or @role='issuer' or @role='authority' or @role='artist' or @role='engraver' or @role='maker'">
							<xsl:value-of select="@role"/>
						</xsl:if>
				</xsl:variable>
				<xsl:choose>					
					<xsl:when test="string($role)">
						<field name="{$role}_facet">
							<xsl:value-of select="normalize-space(.)"/>
						</field>
					</xsl:when>
					<xsl:when test="name() = 'persname' and not(string(@role))">
						<field name="persname_facet">
							<xsl:value-of select="normalize-space(.)"/>
						</field>
					</xsl:when>
				</xsl:choose>
				
				<xsl:if test="name() = 'persname'">
					<xsl:variable name="text-type">
						<xsl:choose>
							<xsl:when test="@role='deity'">
								<xsl:text>deity</xsl:text>
							</xsl:when>
							<xsl:otherwise>
								<xsl:text>persname</xsl:text>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:variable>
					<field name="{$text-type}_text">
						<xsl:if test="@normal">
							<xsl:value-of select="@normal"/>
							<xsl:text> </xsl:text>
						</xsl:if>
						<xsl:value-of select="."/>
						<xsl:text> </xsl:text>
					</field>
				</xsl:if>
				<xsl:if test="name() = 'geogname'">
					<field name="geogname_text">
						<xsl:if test="@normal">
							<xsl:value-of select="@normal"/>
							<xsl:text> </xsl:text>
						</xsl:if>
						<xsl:value-of select="."/>
						<xsl:text> </xsl:text>
					</field>
				</xsl:if>
			</xsl:for-each>

			<xsl:for-each select="descendant::node()/famname">
				<field name="famname_facet">
					<xsl:value-of select="normalize-space(.)"/>
				</field>
			</xsl:for-each>

			<xsl:for-each select="descendant::node()/corpname">
				<xsl:choose>
					<xsl:when test="@role = 'issuer'">
						<field name="issuer_facet">
							<xsl:value-of select="normalize-space(.)"/>
						</field>
					</xsl:when>
					<xsl:when test="@role = 'maker'">
						<field name="maker_facet">
							<xsl:value-of select="normalize-space(.)"/>
						</field>
					</xsl:when>
					<xsl:otherwise>
						<field name="corpname_facet">
							<xsl:value-of select="normalize-space(.)"/>
						</field>
					</xsl:otherwise>
				</xsl:choose>
				
			</xsl:for-each>

			<xsl:for-each select="descendant::node()/subject">
				<xsl:choose>
					<xsl:when test="string(@type)">
						<field name="{@type}_facet">
							<xsl:value-of select="normalize-space(.)"/>
						</field>
					</xsl:when>
					<xsl:otherwise>
						<field name="subject_facet">
							<xsl:value-of select="normalize-space(.)"/>
						</field>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>

			<field name="fulltext">
				<xsl:for-each select="descendant-or-self::text()">
					<xsl:value-of select="normalize-space(.)"/>
					<xsl:text> </xsl:text>
				</xsl:for-each>
				<xsl:for-each select="descendant-or-self::node()[@normal]">
					<xsl:value-of select="@normal"/>
					<xsl:text> </xsl:text>
				</xsl:for-each>
				<xsl:for-each
					select="ancestor::c[@level='series']/descgrp | ancestor::c[@level='series']/bioghist">
					<xsl:for-each select="descendant-or-self::text()">
						<xsl:value-of select="normalize-space(.)"/>
						<xsl:text> </xsl:text>
					</xsl:for-each>
				</xsl:for-each>
				<xsl:for-each
					select="ancestor::c[@level='series']/custodhist | ancestor::c[@level='series']/acqinfo | ancestor::archdesc/custodhist | ancestor::arcdesc/acqinfo">
					<xsl:for-each select="descendant-or-self::text()">
						<xsl:value-of select="normalize-space(.)"/>
						<xsl:text> </xsl:text>
					</xsl:for-each>
				</xsl:for-each>
			</field>
		</doc>
	</xsl:template>

	<xsl:template match="did">
		<xsl:if test="string(normalize-space(unitid))">
			<field name="unitid_text">
				<xsl:value-of select="normalize-space(unitid)"/>
			</field>
			<field name="unitid_display">
				<xsl:value-of select="normalize-space(unitid)"/>
			</field>
		</xsl:if>

		<field name="unittitle_display">
			<xsl:value-of select="normalize-space(unittitle)"/>
		</field>

		<xsl:if test="string(normalize-space(unitdate))">
			<field name="unitdate_display">
				<xsl:value-of select="normalize-space(unitdate)"/>
			</field>
		</xsl:if>

		<xsl:apply-templates select="unitdate"/>
		<xsl:apply-templates select="physdesc"/>
		<xsl:apply-templates select="origination"/>
		<xsl:apply-templates select="daogrp"/>
		<xsl:apply-templates select="materialspec[@type='department']"/>
		<xsl:apply-templates select="materialspec[@type='script']"/>
	</xsl:template>

	<xsl:template match="materialspec[@type='department']">
		<field name="department_facet">
			<xsl:value-of select="normalize-space(text())"/>
		</field>
		<xsl:if test="materialspec[@type='department']">
			<xsl:apply-templates select="materialspec[@type='department']"/>
		</xsl:if>
	</xsl:template>

	<xsl:template match="materialspec[@type='script']">
		<field name="script_facet">
			<xsl:value-of select="normalize-space(text())"/>
		</field>
	</xsl:template>

	<xsl:template match="unitdate">
		<xsl:if test="@normal">
			<xsl:choose>
				<xsl:when test="not(contains(@normal, '/'))">
					<xsl:choose>
						<xsl:when test="contains(@normal, '-')">
							<xsl:variable name="normal">
								<xsl:value-of select="number(substring-after(@normal, '-'))"/>
							</xsl:variable>
							<xsl:variable name="century">
								<xsl:value-of select="ceiling($normal div 100)"/>
							</xsl:variable>
							<field name="era_sint">
								<xsl:text>-</xsl:text>
								<xsl:value-of select="$century"/>
							</field>
						</xsl:when>
						<xsl:when test="not(contains(@normal, '-'))">
							<xsl:variable name="century">
								<xsl:value-of select="ceiling(@normal div 100)"/>
							</xsl:variable>
							<field name="era_sint">
								<xsl:value-of select="$century"/>
							</field>
						</xsl:when>
					</xsl:choose>
				</xsl:when>
				<xsl:when test="contains(@normal, '/')">
					<xsl:variable name="start">
						<xsl:value-of select="number(substring-before(@normal, '/'))"/>
					</xsl:variable>
					<xsl:variable name="end">
						<xsl:value-of select="number(substring-after(@normal, '/'))"/>
					</xsl:variable>
					<xsl:choose>
						<xsl:when test="contains($start, '-')">
							<xsl:variable name="normal">
								<xsl:value-of select="substring-after($start, '-')"/>
							</xsl:variable>
							<xsl:variable name="century">
								<xsl:value-of select="ceiling($normal div 100)"/>
							</xsl:variable>
							<field name="era_sint">
								<xsl:text>-</xsl:text>
								<xsl:value-of select="$century"/>
							</field>
						</xsl:when>
						<xsl:otherwise>
							<xsl:variable name="century">
								<xsl:value-of select="ceiling($start div 100)"/>
							</xsl:variable>
							<field name="era_sint">
								<xsl:value-of select="$century"/>
							</field>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:choose>
						<xsl:when test="contains($end, '-')">
							<xsl:variable name="normal">
								<xsl:value-of select="substring-after($end, '-')"/>
							</xsl:variable>
							<xsl:variable name="century">
								<xsl:value-of select="ceiling($normal div 100)"/>
							</xsl:variable>
							<field name="era_sint">
								<xsl:text>-</xsl:text>
								<xsl:value-of select="$century"/>
							</field>
						</xsl:when>
						<xsl:otherwise>
							<xsl:variable name="century">
								<xsl:value-of select="ceiling($end div 100)"/>
							</xsl:variable>
							<field name="era_sint">
								<xsl:value-of select="$century"/>
							</field>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
			</xsl:choose>

			<xsl:choose>
				<xsl:when test="contains(@normal, '/')">
					<field name="year_sint">
						<xsl:value-of
							select="number(normalize-space(substring-before(@normal, '/')))"/>
					</field>
					<field name="year_sint">
						<xsl:value-of
							select="number(normalize-space(substring-after(@normal, '/')))"/>
					</field>
				</xsl:when>
				<xsl:otherwise>
					<field name="year_sint">
						<xsl:value-of select="number(normalize-space(@normal))"/>
					</field>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
	</xsl:template>

	<xsl:template match="physdesc">
		<xsl:for-each select="descendant::physfacet[@type='material']">
			<field name="material_facet">
				<xsl:value-of select="normalize-space(.)"/>
			</field>
		</xsl:for-each>
		<xsl:if test="string(genreform[@type='format'])">
			<field name="format_facet">
				<xsl:value-of select="normalize-space(genreform[@type='format'])"/>
			</field>
		</xsl:if>
		<xsl:for-each select="genreform[@type='denomination']">
			<field name="denomination_facet">
				<xsl:value-of select="normalize-space(.)"/>
			</field>
		</xsl:for-each>

		<xsl:apply-templates select="physfacet[@type='axis']"/>

		<xsl:if test="number(physfacet[@type='dob'])">
			<field name="dob_sint">
				<xsl:value-of select="normalize-space(physfacet[@type='dob'])"/>
			</field>
		</xsl:if>

		<xsl:if test="physfacet[@type='manufacture']">
			<field name="manufacture_facet">
				<xsl:value-of select="normalize-space(physfacet[@type='manufacture'])"/>
			</field>
		</xsl:if>

		<xsl:if test="physfacet[@type='sernum']">
			<field name="sernum_display">
				<xsl:value-of select="normalize-space(physfacet[@type='sernum'])"/>
			</field>
		</xsl:if>

		<xsl:if test="dimensions">
			<field name="dimensions_sint">
				<xsl:value-of select="normalize-space(dimensions)"/>
			</field>
		</xsl:if>

		<xsl:if test="physfacet[@type='weight']">
			<field name="weight_sint">
				<xsl:value-of select="physfacet[@type='weight']"/>
			</field>
		</xsl:if>

		<xsl:if
			test="string(physfacet[@type='obverse_legend']) or string(physfacet[@type='reverse_legend'])">
			<field name="legend_text">
				<xsl:value-of select="physfacet[@type='obverse_legend']"/>
				<xsl:text> </xsl:text>
				<xsl:value-of select="physfacet[@type='reverse_legend']"/>
				<xsl:text> </xsl:text>
				<xsl:value-of select="physfacet[@type='obverse_legend']/abbr/@expan"/>
				<xsl:text> </xsl:text>
				<xsl:value-of select="physfacet[@type='reverse_legend']/abbr/@expan"/>
			</field>
		</xsl:if>

		<xsl:if test="string(normalize-space(physfacet[@type='obverse_legend']))">
			<field name="obv_leg_display">
				<xsl:value-of select="normalize-space(physfacet[@type='obverse_legend'])"/>
			</field>
		</xsl:if>
		<xsl:if test="string(normalize-space(physfacet[@type='reverse_legend']))">
			<field name="rev_leg_display">
				<xsl:value-of select="normalize-space(physfacet[@type='reverse_legend'])"/>
			</field>
		</xsl:if>
		<xsl:if
			test="string(physfacet[@type='obverse_iconography']) or string(physfacet[@type='reverse_iconography'])">
			<field name="iconography_text">
				<xsl:value-of select="physfacet[@type='obverse_iconography']"/>
				<xsl:text> </xsl:text>
				<xsl:value-of select="physfacet[@type='reverse_iconography']"/>
				<xsl:text> </xsl:text>
			</field>
		</xsl:if>
	</xsl:template>

	<xsl:template match="physfacet[@type='axis']">
		<field name="axis_sint">
			<xsl:choose>
				<xsl:when test="contains(., ':')">
					<xsl:value-of select="normalize-space(substring-before(., ':'))"/>
					<xsl:value-of select="normalize-space(substring-after(., ':'))"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:choose>
						<xsl:when test="string-length(normalize-space(.)) = 2">
							<xsl:value-of select="normalize-space(.)"/>
							<xsl:text>00</xsl:text>
						</xsl:when>
						<xsl:when test="string-length(normalize-space(.)) = 4">
							<xsl:value-of select="normalize-space(.)"/>
						</xsl:when>
					</xsl:choose>
				</xsl:otherwise>
			</xsl:choose>
		</field>
	</xsl:template>

	<xsl:template match="origination">
		<xsl:choose>
			<xsl:when test="geogname[@role='city']">
				<field name="origination_display">
					<xsl:value-of select="geogname[@role='city']"/>
				</field>
			</xsl:when>
			<xsl:when test="geogname[@role='region'] and not(geogname[@role='city'])">
				<field name="origination_display">
					<xsl:value-of select="geogname[@role='region']"/>
				</field>
			</xsl:when>
			<xsl:when test="geogname[@role='state'] and not(geogname[@role='region'])">
				<field name="origination_display">
					<xsl:value-of select="geogname[@role='state']"/>
				</field>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="daogrp">
		<xsl:for-each select="daoloc">
			<field name="{@label}">
				<xsl:value-of select="@href"/>
			</field>
		</xsl:for-each>
	</xsl:template>

	<xsl:template match="bibref">
		<field name="reference_display">
			<xsl:choose>
				<xsl:when test="string(normalize-space(title))">
					<xsl:value-of select="normalize-space(title)"/>
					<xsl:if test="num">
						<xsl:text> no. </xsl:text>
						<xsl:value-of select="normalize-space(num)"/>
						<xsl:text>.</xsl:text>
					</xsl:if>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="normalize-space(.)"/>
				</xsl:otherwise>
			</xsl:choose>
		</field>
	</xsl:template>

</xsl:stylesheet>
